package Communications;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.*;

/**
 *
 * @author shin
 */
public class MultiUnicastServer implements Runnable {

    private int port;
    ServerSocket serverSocket = null;
    private Queue<String> inputStream = null;
    //todo: store message from client to know what to send feedback
    
    public MultiUnicastServer(int port) {
        this.port = port;
        inputStream = new ConcurrentLinkedQueue();
    }

    public Queue<String> getInputStream() {
        return inputStream;
    }

    @Override
    public void run() {
        boolean listening = true;

        try {
            serverSocket = new ServerSocket(port);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(-1);
        }
        try {
            while (listening) {
                new MultiUnicastServerThread(this, serverSocket.accept()).start();
            }
            serverSocket.close();
        } catch (Exception e) {
        }
        
    }
    
    public static void main(String arg[]) {
        MultiUnicastServer s =  new MultiUnicastServer(9999);
        s.run();
    }
}
